public class FrameDiff {
  private PImage prev;
  private int thresh;
  
  public FrameDiff(int w, int h, int t) {
    this.prev = createImage(w,h,RGB);
    this.thresh = t;
  }
  
  public FrameDiff(int w, int h) {
    this(w, h, 60);
  }
  
  public int getThresh() {
    return this.thresh;
  }
  
  public void setThresh(int t) {
    this.thresh = t;
  }
  
  public PImage process(PImage curr) {
    PImage result = createImage(prev.width, prev.height, RGB);
    for (int x = 0; x < prev.width; x++) {
      for (int y = 0; y < prev.height; y++) {
        int curPix = curr.get(x, y);
        int prevPix = prev.get(x, y);
        float err = abs(red(curPix) - red(prevPix)) +
                    abs(green(curPix) - green(prevPix)) +
                    abs(blue(curPix) - blue(prevPix));
        if (err > thresh) result.set(x, y, color(255));
        prev.set(x,y,curPix);
      }
    }
    return result;
  }
}
